package com.ruoyi.business.controller;

import com.ruoyi.bizsys.domain.*;
import com.ruoyi.bizsys.service.ICreditAccountService;
import com.ruoyi.bizsys.service.ICreditApplyService;
import com.ruoyi.bizsys.service.ICreditFlagService;
import com.ruoyi.bizsys.service.ILoanOrderService;
import com.ruoyi.bizsys.utils.BizConstants;
import com.ruoyi.business.common.riskRule.RulePremiseService;
import com.ruoyi.business.service.ICreditService;
import com.ruoyi.business.service.ILoanApplyService;
import com.ruoyi.business.service.ILoanService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.RspEnmu;
import com.ruoyi.common.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Author: cyh
 * @Date: 2021/6/10 11:08 上午
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/manual")
public class ManualAdminController {

    private ICreditService creditService;

    private ILoanApplyService iLoanApplyService;

    private ILoanService loanService;

    private ILoanOrderService loanOrderService;

    private ICreditAccountService creditAccountService;

    protected ICreditApplyService creditApplyService;

    protected ICreditFlagService creditFlagService;


    /**
     * 手动批量授信
     */
    @PreAuthorize("@ss.hasPermi('sys:manual:credit')")
    @PostMapping("credit/{applyNos}")
    @Log(title = "手动批量授信", businessType = BusinessType.INSERT)
    public Object manualBatchCredit(@PathVariable String[] applyNos) {

        Set<String> collect = new HashSet<>(Arrays.asList(applyNos));
        HashMap<Object, Object> resMap = new HashMap<>();

        List<String> accessRes = new ArrayList<>();
        List<Map> errorRes = new ArrayList<>();
        List<String> skipRes = new ArrayList<>();
        for (String applyNo : collect) {
            try {
                //前置校验
                CreditApply creditApply = new CreditApply();
                creditApply.setApplySerialNo(applyNo);
                creditApply.setApplyStatus(BizConstants.APPLY_STATUS_9_APPROVALING);
                List<CreditApply> creditApplies = creditApplyService.selectCreditApplyList(creditApply);
                if (creditApplies.size() == 0) {
                    throw new BusinessException(RspEnmu.SERIAL_NOT_EXIST);
                }
                creditApply = creditApplies.get(0);
                if (BizConstants.APPLY_STATUS_9_APPROVALING != creditApply.getApplyStatus()) {
                    throw new BusinessException(RspEnmu.LOAN_STATUS_IS_NOT_APPROVAL);
                }

                String b = creditService.againTrigger(creditApply);
                if("false".equals(b)){
                    skipRes.add(applyNo);
                }else if("true".equals(b)){
                    accessRes.add(applyNo);
                }else{
                    HashMap<Object, Object> bad = new HashMap<>();
                    bad.put(applyNo, b);
                    errorRes.add(bad);
                }

            } catch (Exception e) {
                log.error("manualBatchCredit 异常：", e);
                HashMap<Object, Object> bad = new HashMap<>();
                bad.put(applyNo, e.getMessage());
                errorRes.add(bad);
            }
        }
        resMap.put("accessRes", accessRes);
        resMap.put("badRes", errorRes);
        resMap.put("skipRes", skipRes);
        return AjaxResult.success(resMap);
    }

    /**
     * 手动批量提现
     */
    @PreAuthorize("@ss.hasPermi('sys:manual:loan')")
    @PostMapping("/loan/{loanNos}")
    @Log(title = "手动批量提现", businessType = BusinessType.INSERT)
    public Object manualBatchLoan(@PathVariable String[] loanNos) {
        HashMap<Object, Object> resMap = new HashMap<>();
        List<String> accessRes = new ArrayList<>();
        List<String> skipRes = new ArrayList<>();
        List<Map> errorRes = new ArrayList<>();

        for (String loanNo : new HashSet<>(Arrays.asList(loanNos))) {
            try {
                LoanOrder loanOrderQuery = new LoanOrder();
                loanOrderQuery.setLoanSerialNo(loanNo);
                List<LoanOrder> loanOrders = loanOrderService.selectLoanOrderList(loanOrderQuery);
                if (loanOrders.size() == 0) {
                    throw new BusinessException(RspEnmu.SERIAL_NOT_EXIST);
                }

                LoanOrder loanOrder = loanOrders.get(0);
                if (LoanOrder.LOAN_STATUS_9_APPROVALING != loanOrder.getLoanStatus()) {
                    throw new BusinessException(RspEnmu.LOAN_STATUS_IS_NOT_APPROVAL);
                }

                String b = loanService.againTrigger(loanOrder);
                if("false".equals(b)){
                    skipRes.add(loanNo);
                }else if("true".equals(b)){
                    accessRes.add(loanNo);
                }else{
                    HashMap<Object, Object> bad = new HashMap<>();
                    bad.put(loanOrder, b);
                    errorRes.add(bad);
                }

            } catch (Exception e) {
                log.error("提现处理异常：", e);
                HashMap<Object, Object> bad = new HashMap<>();
                bad.put(loanNo, e.getMessage());
                errorRes.add(bad);
            }

        }
        resMap.put("accessRes", accessRes);
        resMap.put("badRes", errorRes);
        resMap.put("skipRes", skipRes);
        return AjaxResult.success(resMap);
    }

    public CreditAccount getCreditAccount(String custNo) {
        CreditAccount ca = new CreditAccount();
        ca.setCustNo(custNo);
        ca.setState(BizConstants.BIZ_STATUS_1);
        List<CreditAccount> creditAccountList = creditAccountService.selectCreditAccountList(ca);
        if (CollectionUtils.isEmpty(creditAccountList)) {
            throw new BusinessException(RspEnmu.CUSTOMER_IS_NOT_EXISTS);
        }
        return creditAccountList.get(0);
    }

}
